<!--
> Muaz Khan     - github.com/muaz-khan 
> MIT License   - www.webrtc-experiment.com/licence
> Documentation - www.RTCMultiConnection.org
-->
<h1><a href="http://www.RTCMultiConnection.org/docs/">RTCMultiConnection</a> & Password Protect Rooms</h1>
<button id="setup">Setup RTC-Multi-Connection</button>
<hr />

<script src="//cdn.webrtc-experiment.com/RTCMultiConnection.js">
</script>
<script src="//cdn.webrtc-experiment.com/firebase.js">
</script>
<script>
document.querySelector('#setup').onclick = function() {
    // room password has been set before calling "open" method
    connection.extra.password = prompt('Setup password for your room!');
    connection.open();
    this.disabled = true;
};

var connection = new RTCMultiConnection();

var sessions = {};
connection.onNewSession = function(session) {
    if (sessions[session.sessionid]) return;
    sessions[session.sessionid] = session;

    // set password for person who is trying to join the room
    connection.extra.password = prompt('Enter password to join this room.');
    connection.join(session);
};

connection.onRequest = function(e) {
    // validating password in "onRequest"
    if (e.extra.password != connection.extra.password)
        return alert('password: ' + e.extra.password + ' !== ' + connection.extra.password);

    connection.accept(e);
};

connection.onstream = function(e) {
    document.body.appendChild(e.mediaElement);
};
connection.connect();
</script>
<pre>
    document.querySelector('#setup').onclick = function () { // room password has been set before calling "open" method connection.extra.password = prompt('Setup password for your room!'); connection.open(); }; connection.onNewSession = function (session) { // set password for person who is trying to join the room connection.extra.password = prompt('Enter password to join this room.'); connection.join(session); }; connection.onRequest = function (userid, extra) { // validating password in "onRequest" if (extra.password != connection.extra.password) return alert('password: ' + extra.password + ' !== ' + connection.extra.password); connection.accept(userid, extra); };
</pre>
<hr />
<style>
html {
    background: rgb(255, 252, 239);
}

audio,
video {
    vertical-align: top;
    width: 40%;
}
</style>